﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ZLDC.Common.Estate;
using ZLDC.Topo.Model.Estate;
using ZLDC.Topo.BLL.Estate;

namespace ZLDC.Estate
{
    public partial class HousingManager : _PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnCompetences();   // 权限控制按钮

                hIsUsage.Value = _Session.GetSession("isUsage");                        // 用于前台列表动态列 0表示登录者为住宅业务员 1表示登录者为商铺业务员
                string UsageTypeIDList = _Session.GetSession("UsageTypeIDList");        // 如1,4 表示 住宅、商住的权限(1住宅 2商铺 3写字楼 4商住 5仓库 6厂房 7地皮 8项目盘 9其他* 10民房)

                // 1 超级管理员
                if (info.m_sRoleID != "1")
                {
                    // 根据登录的用户类型 0表示登录者为住宅业务员 1表示登录者为商铺业务员
                    if (hIsUsage.Value == "0")
                        rbUsageType1.Checked = true;
                    else
                        rbUsageType3.Checked = true;

                    // 根据用户授权的用途权限，默认选中哪个类别  rbUsageType1住宅 rbUsageType2写字楼 rbUsageType3商铺 rbUsageType4厂房 rbUsageType5民房  
                    if (UsageTypeIDList.Contains("1"))
                        rbUsageType1.Visible = true;
                    if (UsageTypeIDList.Contains("3"))
                        rbUsageType2.Visible = true;
                    if (UsageTypeIDList.Contains("2"))
                        rbUsageType3.Visible = true;
                    if (UsageTypeIDList.Contains("6"))
                        rbUsageType4.Visible = true;
                    if (UsageTypeIDList.Contains("10"))
                        rbUsageType5.Visible = true;

                    int typeid = _Request.GetInt("typeid", 0);
                    if (typeid == 3)//我的盘源显示出所有类型
                    {
                        rbUsageType1.Visible = true;
                        rbUsageType2.Visible = true;
                        rbUsageType3.Visible = true;
                        rbUsageType4.Visible = true;
                        rbUsageType5.Visible = true;
                    }
                }
                else
                {
                    rbUsageType1.Visible = true;
                    rbUsageType2.Visible = true;
                    rbUsageType3.Visible = true;
                    rbUsageType4.Visible = true;
                    rbUsageType5.Visible = true;
                }

                string usage = _Request.GetString("usage");
                if (usage.Contains("1,4"))
                {
                    rbUsageType1.Checked = true;
                    rbUsageType2.Checked = false;
                    rbUsageType3.Checked = false;
                    rbUsageType4.Checked = false;
                    rbUsageType5.Checked = false;
                }
                else if (usage.Contains("3,4"))
                {
                    rbUsageType2.Checked = true;
                    rbUsageType1.Checked = false;
                    rbUsageType3.Checked = false;
                    rbUsageType4.Checked = false;
                    rbUsageType5.Checked = false;
                }
                else if (usage.Contains("2"))
                {
                    rbUsageType3.Checked = true;
                    rbUsageType1.Checked = false;
                    rbUsageType2.Checked = false;
                    rbUsageType4.Checked = false;
                    rbUsageType5.Checked = false;
                }
                else if (usage == "6")
                {
                    rbUsageType4.Checked = true;
                    rbUsageType1.Checked = false;
                    rbUsageType2.Checked = false;
                    rbUsageType3.Checked = false;
                    rbUsageType5.Checked = false;
                }
                else if (usage == "10")
                {
                    rbUsageType5.Checked = true;
                    rbUsageType1.Checked = false;
                    rbUsageType2.Checked = false;
                    rbUsageType3.Checked = false;
                    rbUsageType4.Checked = false;
                }
            }
        }

        // 权限控制按钮
        private void btnCompetences()
        {
            if (!ZLDC.Common.BaseUser.UserInfo.GetPopedom("添加举报"))
                btnInfo.Visible = false;
            if (!ZLDC.Common.BaseUser.UserInfo.GetPopedom("删除盘源"))
                btnDelete.Visible = false;

            if (!ZLDC.Common.BaseUser.UserInfo.GetPopedom("推荐"))
                btnRecommend.Visible = false;
            if (!ZLDC.Common.BaseUser.UserInfo.GetPopedom("收藏"))
                btnFavorite.Visible = false;
            if (!ZLDC.Common.BaseUser.UserInfo.GetPopedom("封盘"))
                btnLock.Visible = false;

            //if (!ZLDC.Common.BaseUser.UserInfo.GetPopedom("新增盘源"))
            //    HLAddHousing.Visible = false;
            //if (!ZLDC.Common.BaseUser.UserInfo.GetPopedom("盘源导入"))
            //    HLHousingInput.Visible = false;

            if (ZLDC.Common.BaseUser.UserInfo.GetPopedom("修改盘源"))
            {
                hCompetencesIsEdit.Value = "1";
            
                btnUpdate.Visible = true;
            }

            if (!ZLDC.Common.BaseUser.UserInfo.GetPopedom("转无效"))
                btnInvalid.Visible = false;
            if (!ZLDC.Common.BaseUser.UserInfo.GetPopedom("转资料"))
                btnToDatum.Visible = false;
        }

        /// <summary>
        /// 当前转无效盘源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInvalid_Click(object sender, EventArgs e)
        {

            string[] IDList = _Utility.GetStringBySplit(hID.Value.Trim(), '|');


            if (IDList.Length == 0)
            {
                _JavaScript.Alert(this, "请选择盘源");
                return;
            }

            // 1分行经理及以上级别
           string SalesManagerStation = _Utility.GetWebConfigSetting("SalesManagerStation");
            string outmsg = "操作失败！";
            if (IDList != null && IDList.Length > 0)
            {
                for (int i = 0; i < IDList.Length; i++)
                {
                    int id = _Convert.ToInt(IDList[i]);
                    HousingInfo info = CacheHousing.Cache_Detail_Info(id);
                    if (info == null)
                        return;

                    //判断是否是本部盘源并且是本部门管理人员
                    string deptlist = CacheUtility.Cache_GetAllChildDept(this.info.m_sDeptID);

                    if ((deptlist.Contains(info.MarkUDeptId.ToString()) && SalesManagerStation.Contains(_Utility.GetInfoBySession().m_sStation.ToString())) || this.info.m_sRoleID == "1" || _Utility.GetInfoBySession().m_sUserID == info.MarkUId.ToString())
                    {
                        outmsg = ZLDC.Topo.BLL.Estate.HousingBLL.HousingStatus(_Convert.ToInt(IDList[i]), 3, "");
                    }
                }
            }

            _JavaScript.Alert(this, outmsg);


        }

        /// <summary>
        /// 当前转资料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnToDatum_Click(object sender, EventArgs e)
        {
            string[] IDList = _Utility.GetStringBySplit(hID.Value.Trim(), '|');

            if (IDList.Length == 0)
            {
                _JavaScript.Alert(this, "请选择盘源");
                return;
            }

            // 1分行经理及以上级别
            string SalesManagerStation = _Utility.GetWebConfigSetting("SalesManagerStation");
            string outmsg = "操作失败！";
            if (IDList != null && IDList.Length > 0)
            {
                for (int i = 0; i < IDList.Length; i++)
                {
                    int id = _Convert.ToInt(IDList[i]);
                    HousingInfo info = CacheHousing.Cache_Detail_Info(id);
                    if (info == null)
                        return;

                    if (info.Status1 < 2 && info.Protected == 0)
                    {
                        //当前转资料
                        if (info.Status == 1)
                        {
                            //判断是否是本部盘源并且是本部门管理人员
                            string deptlist = CacheUtility.Cache_GetAllChildDept(this.info.m_sDeptID);

                            if ((deptlist.Contains(info.MarkUDeptId.ToString()) && SalesManagerStation.Contains(_Utility.GetInfoBySession().m_sStation.ToString())) || this.info.m_sRoleID == "1" || _Utility.GetInfoBySession().m_sUserID==info.MarkUId.ToString())
                            {
                                outmsg = ZLDC.Topo.BLL.Estate.HousingBLL.HousingStatus(id, 2, "");  // 经理以上级别直接转，添加跟进  
                                //当前转资料清空钥匙，独家，反签
                                
                                ZLDC.Topo.BLL.Estate.HousingBLL.UpdateKeys(id, 0, "", "", "", 0, "", null, 0, "", "");

                                string strinfo = "当前转资料：清空钥匙，独家，反签信息{";
                                if (info.KeepKey != "" && info.KeyNo != "")
                                {
                                    strinfo += "钥匙人：" + info.KeepKey + ",钥匙编号：" + info.KeyNo + ",";
                                }
                                else if (info.OnlyUser != "")
                                {
                                    strinfo += "独家人员：" + info.OnlyUser + ",";
                                }
                                else if (info.CtyContUser != "")
                                {
                                    strinfo += "反签人员：" + info.CtyContUser + ",";
                                }
                                strinfo += "}";
                                if (strinfo.Contains("："))
                                {
                                    HousingFollowBLL.p_housingFollow_save(0, id, "", _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID.ToString()), _Utility.GetInfoBySession().m_sDeptName, _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID.ToString()), _Utility.GetInfoBySession().m_sUserName, strinfo, 0, "");
                                }
                            }
                        }
                    }
                }
            }

            _JavaScript.Alert(this, outmsg);

        }


    }
}
